package com.ltu.m7011e.imagine.login.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.rpc.ServiceDefTarget;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.ltu.m7011e.imagine.sudoku.client.SudokuService;
import com.ltu.m7011e.imagine.sudoku.client.SudokuServiceAsync;

public class Login implements EntryPoint
{
	private AsyncCallback<LoginInfo> callback;
	private LoginServiceAsync reverserService;

	private VerticalPanel panel = new VerticalPanel();
	private HorizontalPanel row1 = new HorizontalPanel();
	private HorizontalPanel row2 = new HorizontalPanel();

	private LoginInfo loginInfo = null;

	@Override
	public void onModuleLoad()
	{
		Label lUsername = new Label("Username:");
		Label lPassword = new Label("Password:");

		TextBox tUsername = new TextBox();
		PasswordTextBox tPassword = new PasswordTextBox();

		Button send = new Button("Sign in");

		row1.add(lUsername);
		row1.add(tUsername);

		row2.add(lPassword);
		row2.add(tPassword);

		panel.add(row1);
		panel.add(row2);
		panel.add(send);
		
		RootPanel.get("content").clear();
		RootPanel.get("content").add(panel);
		
		setUpRPC();
	}

	private void result(LoginInfo data)
	{

	}

	private void setUpRPC()
	{
		reverserService = (LoginServiceAsync) GWT.create(LoginService.class);
		((ServiceDefTarget) reverserService).setServiceEntryPoint(GWT.getModuleBaseURL() + "Login/LoginService");
		callback = new AsyncCallback<LoginInfo>()
		{
			// This method will be called if the service call fails   
			public void onFailure(Throwable caught)
			{
				// Show a message informing the user why the call failed   
				Window.alert("Failed to get response from server" + caught.getMessage());
			}

			// This method will be called if the service call succeeds   
			public void onSuccess(LoginInfo data)
			{
				// Get the service call result and cast it to the desired type and display it
				result(data);
			}
		};
	}
}
